package com.sunset.ai

import io.modelcontextprotocol.server.McpServerFeatures
import io.modelcontextprotocol.spec.McpSchema
import org.springframework.ai.tool.ToolCallbackProvider
import org.springframework.ai.tool.annotation.Tool
import org.springframework.ai.tool.annotation.ToolParam
import org.springframework.ai.tool.method.MethodToolCallbackProvider
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

/**
 * @projectName: ai-mcp-server-demo
 * @description:
 * @author: helinlin
 * @date: 2025/7/3
 */
@Configuration
class ToolConfig {

    @Bean
    fun toolCallbackProvider(): ToolCallbackProvider {
        return MethodToolCallbackProvider.builder().toolObjects(
            AlarmClockService(), OAService()
        ).build()
    }

    @Bean
    fun syncPromptSpecification1(): List<McpServerFeatures.SyncPromptSpecification> {
        return listOf(
            McpServerFeatures.SyncPromptSpecification(
                McpSchema.Prompt("Java专家", "java专家提示词", emptyList<McpSchema.PromptArgument>())
            ) { mcpSyncServerExchange, getPromptRequest ->
                McpSchema.GetPromptResult(
                    "你是一个Java专家,请使用Java领域的内容进行回答",
                    emptyList<McpSchema.PromptMessage>()
                )
            },
            McpServerFeatures.SyncPromptSpecification(
                McpSchema.Prompt("C++专家", "C++专家提示词", emptyList<McpSchema.PromptArgument>())
            ) { mcpSyncServerExchange, getPromptRequest ->
                McpSchema.GetPromptResult(
                    "你是一个C++专家，请使用C++领域的内容进行回答",
                    emptyList<McpSchema.PromptMessage>()
                )
            }
        )
    }

    @Bean
    fun syncResourceSpecification1(): List<McpServerFeatures.SyncResourceSpecification> {
        return listOf(
            McpServerFeatures.SyncResourceSpecification(
                McpSchema.Resource(
                    "/root/home/demo.txt",
                    "demo.txt", "一个demo文件",
                    MediaType.TEXT_PLAIN_VALUE,
                    McpSchema.Annotations(
                        emptyList<McpSchema.Role>(),
                        0.8
                    )
                )
            ) { mcpSyncServerExchange, getResourceRequest ->
                McpSchema.ReadResourceResult(
                    listOf(
                        McpSchema.TextResourceContents(
                            "uri", MediaType.TEXT_PLAIN_VALUE, "hello world!"
                        )
                    )
                )
            })
    }

}

class AlarmClockService {
    @Tool(description = "获取当前的时间")
    fun getCurrentTime(): String {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now())
    }

    @Tool(description = "根据提供的日期设置闹钟,提供的日期格式为：yyyy-MM-dd HH:mm:ss")
    fun setAlarm(@ToolParam(description = "日期") time: String) {
        val alarmTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        log.info("已设置闹钟时间为：{}", alarmTime)
    }
}

class OAService {
    @Tool(description = "获取当前用户信息")
    fun getUserInfo(): UserInfo {
        return UserInfo("helinlin", "男")
    }

    @Tool(description = "获取用户的请假天数")
    fun getLeaveDays(@ToolParam(description = "用户名") userName: String): String {
        return "${userName}已请假10天}"
    }

    @Tool(description = "获取用户日报信息")
    fun getDailyReport(
        @ToolParam(description = "用户名") userName: String,
        @ToolParam(description = "日期，格式为yyyy-MM-dd") date: String,
    ): String {
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        val localDateTime = LocalDate.parse(date, formatter)
        return "${userName},${localDateTime.format(formatter)}的日报是：优化了[具体功能/代码，例如“订单查询接口的响应时间”]，通过[具体优化方法，例如“添加索引和缓存机制”]，性能提升约[具体提升幅度，例如“30%”]"
    }
}

data class UserInfo(val name: String, val gender: String)
